﻿using DevExpress.XtraEditors;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinForm_Mes.ProductManagement.BaseSetting
{
    public partial class AddBaseSettingForm : DevExpress.XtraEditors.XtraForm
    {
        private string baseUrl = "http://localhost:5204";
        private HttpClient httpClient;
        public AddBaseSettingForm()
        {
            InitializeComponent();
            httpClient = new HttpClient();
            simpleButton1.Click+=SimpleButton1_Click;
            simpleButton2.Click+=SimpleButton2_Click;
        }
        private void AddBaseSettingForm_Load(object sender, EventArgs e)
        {

        }
        //提交按钮点击事件
        private async void SimpleButton1_Click(object sender, EventArgs e)
        {
            Random rnd = new Random();
            // 获取表单数据
            var unitsCode =rnd.Next(1000,9999).ToString();
            var unitsName = textEdit1.Text.Trim();
            var unitsRatio=(int)spinEdit1.Value; ;
            var isUsing =radioGroup1.EditValue;
            var isUnits = radioGroup2.EditValue;
            var remark = memoEdit1.Text.Trim();
            // 验证必填字段
            if (string.IsNullOrEmpty(unitsName))
            {
                XtraMessageBox.Show("单位名称不能为空！");
                return;
            }
            try
            {
                // 创建提交的数据对象
                var unitData = new
                {
                    unitsCode= unitsCode,
                    unitsName = unitsName,
                    unitsRatio = unitsRatio,
                    isUsing = isUsing,
                    isUnit = isUnits,
                    remark = remark,
                    CreateUserId = "Admin",
                    UpdateUserId = "Admin"
                };
                var jsonData = JsonConvert.SerializeObject(unitData);
                // 转换为JSON
                var content = new StringContent(
                    JsonConvert.SerializeObject(unitData),
                    Encoding.UTF8,
                    "application/json");

                // 发送POST请求
                HttpResponseMessage response = await httpClient.PostAsync(
                    baseUrl + "/api/Units/AddUnits", content);

                if (response.IsSuccessStatusCode)
                {
                    XtraMessageBox.Show("添加成功！");
                    this.DialogResult = DialogResult.OK;
                    this.Close(); // 关闭窗体
                }
                else
                {
                    XtraMessageBox.Show("添加失败：" + response.ReasonPhrase);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("发生错误：" + ex.Message);
            }
        }
        //取消按钮点击事件
        private async void SimpleButton2_Click(object sender, EventArgs e)
        {
            this.Close(); // 关闭窗体
        }

        private void AddBaseSettingForm_Load_1(object sender, EventArgs e)
        {

        }
    }
}